let imgs = document.querySelectorAll("img");
let list = Array.from({ length: 15 }, (_, i) => i);
let imgList = [];
let i = 0;
async function fetchData() {
  try {
    const response = await fetch("https://dog.ceo/api/breeds/image/random");
    const data = await response.json();
    // console.log(data.message);
    return data.message;
  } catch (error) {
    console.error("Error:", error);
  }
}
async function begin() {
  for (let j = 0; j < 15; j++) {
    let imgUrl = await fetchData();
    imgList.push(imgUrl);
  }
}

async function main() {
  await begin();
//   console.log(imgList);
  let interval = setInterval(() => {
    let idx = popRandomElement();
    imgs[idx].src = imgList[i];
    if (i > 13) {
      clearInterval(interval);
    }
    // console.log(i);
    i++;
  }, 500);
}

main();

// function init() {

// }

function popRandomElement() {
  if (list.length === 0) {
    return;
  }
  // 生成随机索引
  const randomIndex = Math.floor(Math.random() * list.length);

  // 弹出随机元素
  const poppedElement = list.splice(randomIndex, 1)[0];

  return poppedElement;
}

// init();
//
